
**********Employment***********
use "$raw\BlockE0506.dta", clear
**Keeping total employment**
keep if S_no==10
rename avg_pers_workd L
keep DSL L
save "$data\Firm_level_L.dta", replace

**********Capital***********
use "$raw\BlockC0506.dta", clear

keep if S_no==3|S_no==10
keep act_add Open_gross_val S_no DSL
reshape wide Open_gross_val act_add, i(DSL) j(S_no)

rename Open_gross_val3 fixedPlantGrossOpen
rename act_add3 fixedPlantGrossAdd
rename Open_gross_val10 fixedTotalGrossOpen
rename act_add10 fixedTotalGrossAdd

local variables_interest fixedPlantGrossOpen fixedPlantGrossAdd fixedTotalGrossOpen fixedTotalGrossAdd

foreach var of varlist `variables_interest'{
	destring `var', replace
	gen ln_`var'=ln(`var')
	sum `var', d
}
save "$data\asi2005_firm_capital_20240621.dta", replace
****************************Firm details***********

use "$raw\BlockA0506.dta", clear

* Selecting basic sample which drops nics outside 15 to 36
gen nic04_4d = real(substr(trim(Ind_5digit),1,4))
gen nic04_2d = floor(nic04_4d/100)
keep if nic04_2d>=15&nic04_2d<=36

merge 1:1 DSL using "$data\Firm_level_L.dta"
drop if _merge==2
drop _merge

merge 1:1 DSL using "$data\asi2005_firm_capital_20240621.dta"
drop if _merge==2
drop _merge

save "$data\asi2005_firm_level.dta", replace

****************************Domestic Input details***********
use "$raw\BlockH0506.dta", clear
drop if UOM==0
drop if S_no>11

ren Qty_cons productQuantity
label var productQuantity "Qty Made"
ren Purchase_val productValue

merge m:1 DSL using "$data\asi2005_firm_level.dta"
keep if _merge==3
drop _merge
gen asicc=real(Item)

merge m:1 asicc using "$concordances\ASICC_05_06_list.dta"
drop if _merge~=3 /* There were a bunch of ASICC codes which were not present in the datset which is fine I think. */
drop _merge

gen productUnit=UOM
merge m:1 productUnit using "$concordances\asi_productunits_conc.dta"
ren productUnit inputUnit 
ren productUnit_con inputUnit_con
ren productQty_multfactor inputQty_multfactor
drop if _merge~=3
drop _merge /* There were 5 uncommon units which didn't show up in the full */

gen input_unit_cat = asicc*100 +  inputUnit_con
gen  inputQuantity_con =  productQuantity* inputQty_multfactor
gen price = Rate/inputQty_multfactor
gen price2 = productValue/ inputQuantity_con
compare price price2
gen ratio_test = price/price2 -1
su ratio_test, de  /* At most 20% differ a lot */
gen ln_price = log(price)
gen ln_price2 = log(price2)
gen ln_L = log(L)
drop if ln_L==.

// rename DSL fid
drop if Rate==0.00

xtset input_unit_cat
xtreg ln_price ln_L if ln_price2~=.&ln_price~=., fe r
xtreg ln_price2 ln_L if ln_price2~=.&ln_price~=., fe r

save "$data\asi2005_inputlevel04082013.dta", replace

****************************Import Input details***********
// use "$raw\BlockI0506.dta", clear
// * Dropping missing product data as well as products which report units as others
// drop if S_no>11
// merge m:1 DSL using "$data\asi2005_firm_level.dta"
// keep if _merge==3
// drop _merge
// gen asicc=real(Item)
//
// merge m:1 asicc using "$concordances\ASICC_05_06_list.dta"
// drop if _merge~=3 /* There were a bunch of ASICC codes which were not present in the datset which is fine I think. */
// tab  inputUnit if inputUnit~=  unitcode_ASI_05
// drop if inputUnit==0
// drop if inputUnit~=  unitcode_ASI_05
// drop _merge


****************************Product details***********
use "$raw\BlockJ0506.dta", clear
ren Qty_mfd productQuantity
label var productQuantity "Qty Made"
ren Ex_fact_val_output productValue
label var productValue "Ex Factory Value"
merge m:1 DSL using "$data\asi2005_firm_level.dta"
keep if _merge==3
drop _merge
gen asicc=real(Item)

* Dropping products whose units are not standardized or missing
drop if UOM==0|UOM==999

merge m:1 asicc using "$concordances\ASICC_05_06_list.dta"
drop if _merge~=3 /* There were a bunch of ASICC codes which are present in the datset. */
drop _merge

gen productUnit=UOM
* Merge the units in the ASI to their reduced concordance and also the multiplicative factor needed for qty's to convert to the concorded units.
merge m:1 productUnit using "$concordances\asi_productunits_conc.dta"
drop if _merge~=3
drop _merge 

* Generating 2 prices
gen survey = "ASI"
gen product_unit_cat = asicc*100 +  productUnit_con
gen  productQuantity_con =  productQuantity* productQty_multfactor
gen price = productValue/productQuantity_con
// destring productNetValue, gen(price2)
// replace price2 = price2/productQty_multfactor
// compare price price2
// gen ratio_test = price/price2 -1
// su ratio_test, de   /* At most 20% differ a lot */
gen ln_price = log(price)
// gen ln_price2 = log(price2)
gen ln_L = log(L)
drop if ln_L==.

xtset product_unit_cat
xtreg ln_price ln_L if survey=="ASI"&ln_price~=., fe r

save "$data\asi2005_productlevel04082013.dta", replace
// ^
//
// use "$raw\BlockD0506.dta", clear
//
//
//
//
// use "$raw\BlockJ0506.dta", clear
//
// 
// ren Qty_mfd productQuantity
// label var productQuantity "Qty Made"
// ren Ex_fact_val_output productValue
// label var productValue "Ex Factory Value"
//
// merge m:1 DSL using "$raw\BlockA0506.dta"